package com.extension.company.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.extension.company.admin.R;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:zwk
 * 联系方式：497981239@qq.com
 * 时间：on 2019/6/26 20:34
 * 功能：返回的图片的适配器
 */
public class FeedBackRecycleViewAdapter extends RecyclerView.Adapter<FeedBackRecycleViewAdapter.FeedBackHolder> {

	public static final int TYPE_CAMERA = 1; // 添加图片
	public static final int TYPE_PICTURE = 2; // 图片
	private Context context;
	private LayoutInflater mInflator;
	private int setSelectMax = 4; // 最大选择的数量
	private List<LocalMedia> list = new ArrayList<>();

	public FeedBackRecycleViewAdapter(Context context, onAddPicClickListener mOnAddPicClickListener) {
		this.context = context;
		this.mOnAddPicClickListener = mOnAddPicClickListener;
		this.mInflator = LayoutInflater.from(context);
	}

	@NonNull
	@Override
	public FeedBackHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
		View view = mInflator.inflate(R.layout.feed_back_img_item, viewGroup, false);
		FeedBackHolder viewHolder = new FeedBackHolder(view);
		return viewHolder;
	}

	@Override
	public void onBindViewHolder(@NonNull final FeedBackHolder feedBackHolder, int i) {
		if (getItemViewType(i) == TYPE_CAMERA) {  // 显示添加图片
			feedBackHolder.mImg.setImageResource(R.drawable.tianjiatup);
			feedBackHolder.mImg.setOnClickListener(new View.OnClickListener() { // 照片添加
				@Override
				public void onClick(View view) {
					mOnAddPicClickListener.onAddPicClick();
				}
			});
			feedBackHolder.ll_del.setVisibility(View.INVISIBLE);

		} else {    // 显示正常的图片的显示
			feedBackHolder.ll_del.setVisibility(View.VISIBLE);
			feedBackHolder.ll_del.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					int index = feedBackHolder.getAdapterPosition();
					if (index != RecyclerView.NO_POSITION) {
						list.remove(index); // 删除，先删除list中的数据
						notifyItemRemoved(index);
						notifyItemRangeChanged(index, list.size());  // 更新 index 到最后的数据
					}
				}
			});

			LocalMedia media = list.get(i); // 获取i位置的数据的 类型
			int mimeType = media.getMimeType();
			String path = media.getPath(); // 获取i 位置上图片的原图地址

			RequestOptions options = new RequestOptions()
					.centerCrop()
					.placeholder(R.color.color_f6)
					.diskCacheStrategy(DiskCacheStrategy.ALL); // 缓存全部尺寸的图片

			Glide.with(feedBackHolder.itemView.getContext())
					.load(path)
					.apply(options)
					.into(feedBackHolder.mImg);
			if (mItemClickListener != null) {
				feedBackHolder.itemView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						int position = feedBackHolder.getAdapterPosition();
						mItemClickListener.onItemClick(position, view);
					}
				});
			}


		}
	}

	@Override
	public int getItemCount() {
		if (list.size() < setSelectMax) { //选中的数据小于 最大的数据时，多显示一条，展示添加按钮
			return list.size() + 1;
		} else {
			return list.size();  // 数量>= 最大数量的时候，显示所有的照片。
		}
	}

	@Override
	public int getItemViewType(int position) {
		if (isShowAddItem(position)) {
			return TYPE_CAMERA;
		} else {
			return TYPE_PICTURE;
		}
	}


	public void setSelectMax(int selectMax) {
		this.setSelectMax = selectMax;
	}

	public void setList(List<LocalMedia> list) {
		this.list = list;
	}

	/**
	 * 是否显示添加按钮
	 *
	 * @param position 位置
	 * @return true
	 */
	private boolean isShowAddItem(int position) {
		int size = list.size() == 0 ? 0 : list.size();
		return position == size;
	}

	class FeedBackHolder extends RecyclerView.ViewHolder {
		ImageView mImg;
		LinearLayout ll_del;

		public FeedBackHolder(@NonNull View itemView) {
			super(itemView);
			mImg = (ImageView) itemView.findViewById(R.id.fiv);  // 图片
			ll_del = (LinearLayout) itemView.findViewById(R.id.ll_del);  // 图片上的x的按钮
		}
	}

	private onAddPicClickListener mOnAddPicClickListener;

	public interface onAddPicClickListener {  // 点击添加图片
		void onAddPicClick();
	}


	protected OnItemClickListener mItemClickListener;

	public interface OnItemClickListener {
		void onItemClick(int position, View v);
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		this.mItemClickListener = listener;
	}
}
